home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / Utilities / CalibrateLuminance / GetALuminance.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-17  |  3.2 KB  |  105 lines  |  [TEXT/KAHL]

  1. /*
  2. GetALuminance.c
  3. © 1990-1992 Denis G. Pelli
  4. Two subroutines used by CalibrateLuminance.c. Probably not useful outside that context.
  5.  
  6. HISTORY:
  7. 7/28/90    dgp    wrote it.
  8. 9/24/90    dgp    changed nBackground to VBackground.
  9. 6/25/91 dgp fixed bug pointed out by Sujeet Paul whereby LToVHunt() made a superfluous
  10. call to GetALuminance() with frames=1, forcing use of the A/D even if it wasn't available.
  11. On a machine without an A/D card this would cause CalibrateLuminance to fail.
  12. On machines with an A/D card this wasted a second on each call to LToVHunt().
  13. 8/24/91    dgp    Made compatible with THINK C 5.0.
  14. 12/17/92 dgp Enhanced to support arbitrary dacSize. Replaced 256 by LP->VMax+1 
  15. in LToVHunt.
  16. 12/21/92 dgp No longer load unused dac bits.
  17. */
  18. #include "VideoToolbox.h"
  19. #include "Luminance.h"
  20.  
  21. double GetALuminance(LuminanceRecord *LP,GDHandle device,
  22.     int frames,double LuminancePerVoltage,int entry,int red,int green,int blue);
  23. void LToVHunt(LuminanceRecord *LP,GDHandle device,CWindowPtr window,
  24.     double LuminancePerVoltage,int frames,double darkLuminance);
  25.  
  26.  
  27. double GetALuminance(LuminanceRecord *LP,GDHandle device,
  28.     int frames,double LuminancePerVoltage,int entry,int red,int green,int blue)
  29. /*
  30. Get luminance produced by an RGB triplet. If frames==0 then ask for manual
  31. reading. If frames!=0 then use A/D to make automatic reading.
  32. */
  33. {
  34.     static double L;
  35.     static char string[100];
  36.     long finalTicks;
  37.     short left;
  38.     #if __MWERKS__
  39.         extern long __SIOUXtextWindow;
  40.     #endif
  41.  
  42.     LP->dacSize=Log2L(LP->VMax*2-1);    // in bits, rounded up
  43.     left=LP->leftShift=16-LP->dacSize;
  44.     LP->table[entry].rgb.red   = red<<left;
  45.     LP->table[entry].rgb.green = green<<left;
  46.     LP->table[entry].rgb.blue  = blue<<left;
  47.     LoadLuminances(device,LP,entry,entry);        /* load clut entry */
  48.     if(frames){
  49.         Delay(60L,&finalTicks);                    /* wait for photometer to settle */
  50.         LoadLuminances(device,LP,entry,entry);    /* synchronize to display */
  51.         L=VoltsDuringFrame(frames)*LuminancePerVoltage;
  52.     }
  53.     else{
  54.         printf("Please enter luminance in %s (%4.1f):",LP->units,L);
  55.         #if __MWERKS__
  56.             SelectWindow((WindowPtr)__SIOUXtextWindow);
  57.         #endif
  58.         gets(string);
  59.         sscanf((char *)string,"%lf",&L);
  60.     }
  61.     return L;
  62. }
  63.  
  64.  
  65. void LToVHunt(LuminanceRecord *LP,GDHandle device,CWindowPtr window,
  66.     double LuminancePerVoltage,int frames,double darkLuminance)
  67. /*
  68. finds the DAC setting j, such that the desired luminance is
  69. between Lj and Lj+1. This is achieved by actually measuring
  70. the resulting luminances, before the LuminanceRecord is
  71. fully defined. The search proceeds by bisection:
  72. LP->LBackground==desired luminance;
  73. LP->VBackground==j;
  74. */
  75. {
  76.     int ju,jm,jl;
  77.     double LMeasured;
  78.     WindowPtr oldWindow;
  79.     int index=1;
  80.  
  81.     GetPort(&oldWindow);
  82.     SetPort((WindowPtr)window);
  83.     BringToFront((WindowPtr)window);
  84.     PmBackColor(index);
  85.     EraseRect(&window->portRect);
  86.     jl=0;
  87.     ju=LP->VMax+1;
  88.     while (ju-jl > 1) {
  89.         jm=(ju+jl)/2;
  90.         LMeasured=GetALuminance(LP,device,frames,LuminancePerVoltage
  91.             ,index,jm,jm,jm);
  92.         LMeasured-=darkLuminance;
  93.         if (LP->LBackground > LMeasured)
  94.             jl=jm;
  95.         else
  96.             ju=jm;
  97.     }
  98.     LP->VBackground=jl;
  99.     LP->table[index].rgb=(**(**(**device).gdPMap).pmTable).ctTable[index].rgb;
  100.     LoadLuminances(device,LP,index,index);        /* restore clut entry */
  101.     SendBehind((WindowPtr)window,NULL);
  102.     SetPort(oldWindow);
  103.     return;
  104. }
  105.